/*
 * Copyright 2011 Jin Kwon <jinahya at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.googlecode.flac4j.format.metadata;


import com.googlecode.jinahya.io.BitInput;
import com.googlecode.jinahya.io.BitOutput;
import com.googlecode.jinahya.util.BitAccessible;

import java.io.IOException;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlType(propOrder = {"offset", "number"})
public class Index implements BitAccessible {


    /** GENERATED. */
    private static final long serialVersionUID = -709177340414081052L;


    /** fixed size. */
    protected static final int LENGTH_IN_BYTES = 96;


    /** CD-DA pre-gap. */
    public static final int CD_DA_PRE_GAP = 0;


    @Override
    public void read(final BitInput input) throws IOException {

        setOffset(input.readLong(64));
        setNumber(input.readUnsignedInt(8));

        // ------------------------------------------------------------ RESERVED
        for (int i = 0; i < 3; i++) {
            input.readUnsignedInt(8);
        }
        // ------------------------------------------------------------ RESERVED
    }


    @Override
    public void write(final BitOutput output) throws IOException {

        output.writeLong(64, getOffset());
        output.writeUnsignedInt(8, getNumber());

        // ------------------------------------------------------------ RESERVED
        for (int i = 0; i < 3; i++) {
            output.writeUnsignedInt(8, 0x00);
        }
        // ------------------------------------------------------------ RESERVED
    }


    /**
     * Returns parent track.
     *
     * @return track
     */
    public Track getTrack() {
        return track;
    }


    /**
     * Sets parent track.
     *
     * @param track track
     */
    public void setTrack(final Track track) {

        if (track == null) {
            throw new NullPointerException("null track");
        }

        this.track = track;
    }


    /**
     * Returns offset.
     *
     * @return offset
     */
    public long getOffset() {
        return offset;
    }


    /**
     * Sets offset.
     *
     * @param offset offset
     */
    public void setOffset(final long offset) {

        if (offset < 0L) {
            throw new IllegalArgumentException("offset(" + offset + ") < 0L");
        }

        this.offset = offset;
    }


    /**
     * Returns number.
     *
     * @return number
     */
    public int getNumber() {
        return number;
    }


    /**
     * Sets number.
     *
     * @param number number
     */
    public void setNumber(final int number) {

        if (number < 0) {
            throw new IllegalArgumentException("number(" + number + ") < 0");
        }

        this.number = number;
    }


    /** parent track. */
    @XmlTransient
    private Track track;


    /** offset in samples. */
    @XmlElement(required = true)
    private long offset;


    /** index point number. */
    @XmlElement(required = true)
    private int number;


}

